package com.example.xmpic.ui.activity.local;

import android.content.Intent;
import android.os.Message;
import android.util.Log;
import android.view.View;

import com.example.common.base.BaseBindActivity;
import com.example.common.utils.LiveDataBus;
import com.example.common.view.refresh.CustomRefreshView;
import com.example.xmpic.R;
import com.example.xmpic.databinding.ActivityDevicePictureListBinding;
import com.example.xmpic.support.FunPath;
import com.example.xmpic.support.FunSupport;
import com.example.xmpic.support.OnFunDeviceOptListener;
import com.example.xmpic.support.config.OPCompressPic;
import com.example.xmpic.support.models.FunDevice;
import com.example.xmpic.support.models.FunFileData;
import com.example.xmpic.ui.adapter.DeviceCameraPicAdapter;
import com.lib.IFunSDKResult;
import com.lib.MsgContent;
import com.lib.SDKCONST;
import com.lib.sdk.struct.H264_DVR_FILE_DATA;
import com.lib.sdk.struct.H264_DVR_FINDINFO;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author skygge
 * @date 2019-12-04.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：远程设备图片列表
 */

public class CameraPictureListActivity extends BaseBindActivity<ActivityDevicePictureListBinding> implements OnFunDeviceOptListener, IFunSDKResult{
    private FunDevice mFunDevice = null;
    private H264_DVR_FINDINFO findInfo = null;
    private int pictureType = SDKCONST.SDK_File_Type.SDK_PIC_ALL;
    private List<FunFileData> mDatas = new ArrayList<>();
    private DeviceCameraPicAdapter mDeviceCameraPicAdapter;
    private Calendar calendar;
    private final int REQUEST_SELECT_DATE = 1;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_device_picture_list;
    }

    @Override
    protected void initialize() {
        super.initialize();
        int devId = getIntent().getIntExtra("FUN_DEVICE_ID", 0);
        mFunDevice = FunSupport.getInstance().findDeviceById(devId);
        if (null == mFunDevice) {
            finish();
        }
    }

    @Override
    protected void initView() {
        super.initView();
        FunPath.onCreateSptTempPath(mFunDevice.getSerialNo());
        FunSupport.getInstance().registerOnFunDeviceOptListener(this);
        calendar = Calendar.getInstance();
        mDeviceCameraPicAdapter = new DeviceCameraPicAdapter(this, mDBind.pictureList.getRecyclerView(), mFunDevice);
        mDBind.pictureList.getRecyclerView().setAdapter(mDeviceCameraPicAdapter);
    }

    @Override
    protected void initData() {
        super.initData();
        onSearchPicture(calendar.getTime(), 0);
    }

    @Override
    protected void initDataBus() {
        super.initDataBus();
        LiveDataBus.get().with("FUN_PICTURE_CLICK", Integer.class).observe(this, integer -> {
            Intent intent = new Intent(this, CameraNormalPicActivity.class);
            intent.putExtra("position", integer);
            intent.putExtra("FUN_DEVICE_ID", mFunDevice.getId());
            startActivity(intent);
        });
    }

    @Override
    protected void initListener() {
        super.initListener();
        mDBind.ivPictureBack.setOnClickListener(view -> finish());
        mDBind.ivPictureTime.setOnClickListener(view -> {
            Intent intent = new Intent(this, DateSelectActivity.class);
            intent.putExtra("FUN_DEVICE_ID", mFunDevice.getId());
            intent.putExtra("FUN_FIND_TYPE", false);
            intent.putExtra("Date", calendar);
            startActivityForResult(intent,REQUEST_SELECT_DATE);
        });
        mDBind.pictureList.setOnLoadListener(new CustomRefreshView.OnLoadListener() {
            @Override
            public void onRefresh() {
                onSearchMorePicture(calendar.getTime(), 0);
                mDBind.pictureList.complete();
            }

            @Override
            public void onLoadMore() {
                onSearchMorePicture(calendar.getTime(), 1);
            }
        });
    }

    private void onSearchPicture(Date date, int flag) {
        showProgressDialog();
        findInfo = new H264_DVR_FINDINFO();
        findInfo.st_1_nFileType = pictureType;
        initSearchInfo(findInfo, date, 0, flag);
        FunSupport.getInstance().requestDeviceFileList(mFunDevice, findInfo);
    }
    
    private void onSearchMorePicture(Date date, int flag){
    	findInfo = new H264_DVR_FINDINFO();
    	findInfo.st_1_nFileType = pictureType;
    	initSearchInfo(findInfo, date, 0, flag);
    	FunSupport.getInstance().requestDeviceFileList(mFunDevice, findInfo);
    }

    private void initSearchInfo(H264_DVR_FINDINFO info, Date date, int channel, int flag) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        info.st_2_startTime.st_0_dwYear = c.get(Calendar.YEAR);
        info.st_2_startTime.st_1_dwMonth = c.get(Calendar.MONTH) + 1;
        info.st_2_startTime.st_2_dwDay = c.get(Calendar.DATE);
        if (flag == 0) {
        	info.st_2_startTime.st_3_dwHour = 0;
        	info.st_2_startTime.st_4_dwMinute = 0;
        	info.st_2_startTime.st_5_dwSecond = 0;
		}else {
			info.st_2_startTime.st_3_dwHour = c.get(Calendar.HOUR_OF_DAY);
			info.st_2_startTime.st_4_dwMinute = c.get(Calendar.MINUTE);
			info.st_2_startTime.st_5_dwSecond = c.get(Calendar.SECOND);
		}
        info.st_3_endTime.st_0_dwYear = c.get(Calendar.YEAR);
        info.st_3_endTime.st_1_dwMonth = c.get(Calendar.MONTH) + 1;
        info.st_3_endTime.st_2_dwDay = c.get(Calendar.DATE);
        info.st_3_endTime.st_3_dwHour = 23;
        info.st_3_endTime.st_4_dwMinute = 59;
        info.st_3_endTime.st_5_dwSecond = 59;
        info.st_0_nChannelN0 = channel;
    }


    @Override
    public void onDeviceLoginSuccess(FunDevice funDevice) {

    }

    @Override
    public void onDeviceLoginFailed(FunDevice funDevice, Integer errCode) {

    }

    @Override
    public void onDeviceGetConfigSuccess(FunDevice funDevice, String configName, int nSeq) {
    }

    @Override
    public void onDeviceGetConfigFailed(FunDevice funDevice, Integer errCode) {

    }

    @Override
    public void onDeviceSetConfigSuccess(final FunDevice funDevice,
    		final String configName) {

    }

    @Override
    public void onDeviceSetConfigFailed(final FunDevice funDevice,
    		final String configName, final Integer errCode) {

    }

    @Override
    public void onDeviceChangeInfoSuccess(final FunDevice funDevice) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onDeviceChangeInfoFailed(final FunDevice funDevice, final Integer errCode) {
        // TODO Auto-generated method stub
    }
    
    @Override
    public void onDeviceOptionSuccess(final FunDevice funDevice, final String option) {
		// TODO Auto-generated method stub
	}
	
    @Override
	public void onDeviceOptionFailed(final FunDevice funDevice, final String option, final Integer errCode) {
		// TODO Auto-generated method stub
	}

    @Override
    public void onDeviceFileListChanged(FunDevice funDevice) {
        Log.i(TAG,"onDeviceFileListChanged1");
    }

    @Override
    public void onDeviceFileListChanged(FunDevice funDevice, H264_DVR_FILE_DATA[] datas) {
        dismissProgressDialog();
        if (null != funDevice && null != mFunDevice && funDevice.getId() == mFunDevice.getId()) {
            mDatas.clear();
            for (H264_DVR_FILE_DATA data : datas) {
                FunFileData funFileData = new FunFileData(data, new OPCompressPic());
                mDatas.add(funFileData);
            }
            mFunDevice.mDatas = mDatas;
        	if (mDatas.size()>0){
                FunFileData data = mDatas.get(mDatas.size()-1);
                calendar.setTime(data.getBeginDate());
                mDBind.pictureEmpty.setVisibility(View.GONE);
                mDBind.pictureList.setVisibility(View.VISIBLE);
                mDeviceCameraPicAdapter.setFunFileData(mDatas);
            } else {
                showToast(getString(R.string.device_pic_list_empty));
                mDeviceCameraPicAdapter.setFunFileData(null);
                mDBind.pictureList.setVisibility(View.GONE);
                mDBind.pictureEmpty.setVisibility(View.VISIBLE);
            }
        }
    }

	@Override
	public int OnFunSDKResult(Message arg0, MsgContent arg1) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void onDeviceFileListGetFailed(FunDevice funDevice) {
		// TODO Auto-generated method stub
        mDeviceCameraPicAdapter.setFunFileData(null);
        mDBind.pictureList.setVisibility(View.GONE);
        mDBind.pictureEmpty.setVisibility(View.VISIBLE);
        dismissProgressDialog();
	}

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode!=RESULT_OK) return;

        if(requestCode==REQUEST_SELECT_DATE){
            int result_year = data.getExtras().getInt("year");
            int result_month = data.getExtras().getInt("month");
            int result_day   = data.getExtras().getInt("day");
            calendar.set(result_year, result_month, result_day);
            onSearchPicture(calendar.getTime(), 0);
        }
    }

    @Override
    protected void onDestroy() {
        FunSupport.getInstance().removeOnFunDeviceOptListener(this);
        if (null != mDeviceCameraPicAdapter) {
            mDeviceCameraPicAdapter.release();
        }
        super.onDestroy();
    }
}
